package com.ls.thread;

import java.util.Date;
import java.util.Random;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.Executors;
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.ScheduledFuture;
import java.util.concurrent.TimeUnit;

public class ExecutorException {

	public static void main(String[] args) {
		ScheduledExecutorService  executorService = Executors.newScheduledThreadPool(2);
		ScheduledFuture<?> result = executorService.scheduleAtFixedRate(new Task(), 1, 1, TimeUnit.SECONDS);
		try {
			Thread.sleep(2000);
			result.cancel(true);
			System.out.println(result.isCancelled());
			 result = executorService.scheduleAtFixedRate(new Task(), 1, 1, TimeUnit.SECONDS);
			 Thread.sleep(2000);
			 result.cancel(true);
		} catch (InterruptedException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
	
}
class Task implements Runnable{

	public void run() {
		System.out.println("任务执行 "+new Date());
//		if (new Random().nextBoolean()){
//			throw new RuntimeException("异常退出，后面无法继续执行");
//		}
		System.out.println("没有抛出异常 "+new Date());
	}
	
}
